<?php

namespace Mouta\AmistateBundle\Controller;

use Sonata\AdminBundle\Controller\CRUDController as Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;

class SonataAdminCommentController extends Controller
{
    public function deleteAction($id)
    {
        $id = $this->get('request')->get($this->admin->getIdParameter());
        /* @var $object \Mouta\AmistateBundle\Entity\Comment */
        $object = $this->admin->getObject($id);

        if (!$object) {
            throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
        }

        if (false === $this->admin->isGranted('DELETE', $object)) {
            throw new AccessDeniedException();
        }

        if ($this->getRequest()->getMethod() == 'DELETE') {
            try {
                $this->procesarBorrado($object);
                $this->get('session')->setFlash('sonata_flash_success', 'flash_delete_success');
            } catch ( ModelManagerException $e ) {
                $this->get('session')->setFlash('sonata_flash_error', 'flash_delete_error');
            }

            return new RedirectResponse($this->admin->generateUrl('list', $this->admin->getFilterParameters()));
        }

        return $this->render('SonataAdminBundle:CRUD:delete.html.twig', array(
            'object' => $object,
            'action' => 'delete'
        ));
    }
    
    /**
     * execute a batch delete
     *
     * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
     *
     * @param \Sonata\AdminBundle\Datagrid\ProxyQueryInterface $query
     *
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
     */
    public function batchActionDelete(ProxyQueryInterface $query)
    {
        $posts = $this->get('request')->request->get('idx');
        foreach($posts as $post){
            $object = $this->admin->getObject($post);
            $this->procesarBorrado($object);
        }
        return new RedirectResponse($this->admin->generateUrl('list', $this->admin->getFilterParameters()));
    }
    
    protected function procesarBorrado(\Mouta\AmistateBundle\Entity\Comment $object){
        $thread = $object->getThread();
        $comments = $this->container->get('fos_comment.manager.comment')->findCommentsByThread($thread);
        foreach($comments as $comment){
            /* @var $comment \Mouta\AmistateBundle\Entity\Comment */
            $ancestors = array();
            foreach($comment->getAncestors() as $ancestor){
                if($ancestor != $object->getId()){
                    $ancestors[] = $ancestor;
                }
            }
            $comment->setAncestors($ancestors);
            $this->container->get('fos_comment.manager.comment')->saveComment($comment);
        }
        $this->admin->delete($object);
        $thread->setNumComments(count($comments)-1);
        $this->container->get('fos_comment.manager.thread')->saveThread($thread);
    }
}